#!/usr/bin/env node

const http = require('http'),
  fs = require('fs'),
  log = console.log;
http.createServer((req,res)=>{
  log(`${req.method} ${req.url} HTTP/${req.httpVersion}`);
  log(req.headers);
  log('');

  let file = __dirname + req.url;
  fs.readFile(file,(err,data)=>{
    if(err){
      log(err.message);
      res.statusCode = 404;
      res.end(err.message);
    }else{
      res.end(data);
    }
  });

}).listen(8080);

//const server = http.createServer();
//server.listen(8080);
//server.on('request',(req,res)=>{
//  log(`${req.method} ${req.url} HTTP/${req.httpVersion}`);
//  log(req.headers);
//  log('')

//  const file = __dirname + req.url;
  
//fs.readFile(file,(err,data)=>{
//  if(err){
//    log(err.message);
//    res.statusCode = 404;
//    res.end(err.message);
//  }else{
//    res.end(data);
//  }
//})
//  res.end(fs.readFileSync(file).toString('utf8'));
//  res.write('hello');
//  res.end('bye');
//}).listen(8080);

